package controller

import (
	"gitee.com/zhoulvvv/my_go_gateway/dto"
	"gitee.com/zhoulvvv/my_go_gateway/middleware"
	"gitee.com/zhoulvvv/my_go_gateway/services"
	"github.com/gin-gonic/gin"
)

type OAuthController struct {
	OAuthSvc *services.OAuthServices
}

func RegisterOAuthController(router *gin.RouterGroup) {
	oauth := &OAuthController{}
	oauthSvc, err := services.NewOAuthServices()
	if err != nil {
		panic("OAuthService初始化失败：" + err.Error())
	}
	oauth.OAuthSvc = oauthSvc
	router.POST("/tokens", oauth.Tokens)
}

// Tokens godoc
// @Summary 获取TOKEN
// @Description 获取TOKEN
// @Tags OAUTH
// @ID /oauth/tokens
// @Accept  json
// @Produce  json
// @Param body body dto.TokensInput true "body"
// @Success 200 {object} middleware.Response{data=dto.TokensOutput} "success"
// @Router /oauth/tokens [get]
func (o *OAuthController) Tokens(c *gin.Context) {
	params := &dto.TokensInput{}
	if err := params.BindValidaParam(c); err != nil {
		middleware.ResponseError(c, 6000, err)
		return
	}
	outParams, err := o.OAuthSvc.GetTokens(c, params)
	if err != nil {
		middleware.ResponseError(c, 6000, err)
		return
	}
	middleware.ResponseSuccess(c, outParams)
}
